package com.jzo2o.orders.manager.service.client;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.jzo2o.api.customer.AddressBookApi;
import com.jzo2o.api.customer.dto.response.AddressBookResDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * 用户服务：熔断降级处理类
 *
 * @author zgp
 * @version V1.0
 * @date 2025-04-01 21:01
 */
@Component
@Slf4j
public class CustomersClient {
    @Resource
    private AddressBookApi addressBookApi;

    /**
     * 根据地址id查询地址详情
     * @param addressBookId 地址id
     * @return
     */
    @SentinelResource(value = "searchAddressDetail", fallback = "searchAddressDetailFallback", blockHandler = "searchAddressDetailBlock")
    public AddressBookResDTO getDetailById(Long addressBookId){
        return addressBookApi.getDetailById(addressBookId);
    }

    /**
     * 查询地址详情降级处理
     * @param e
     * @return
     */
    public AddressBookResDTO searchAddressDetailFallback(Throwable e){
        log.error("远程调用addressBookApi.getDetailById(Long addressBookId) 方法失败降级，{}", e.getMessage());
        AddressBookResDTO addressBookResDTO = new AddressBookResDTO();
        addressBookResDTO.setId(-1L);
        return addressBookResDTO;
    }

    /**
     * 查询地址详情熔断处理
     * @param e
     * @return
     */
    public AddressBookResDTO searchAddressDetailBlock(BlockException e){
        log.error("远程调用addressBookApi.getDetailById(Long addressBookId) 方法失败熔断，{}", e.getMessage());
        AddressBookResDTO addressBookResDTO = new AddressBookResDTO();
        addressBookResDTO.setId(-2L);
        return addressBookResDTO;
    }

}
